﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

//
namespace Hypermarket_Serial_Port
{
    class CancelTransactionManager : CashierRegisterTaskManager
    {

        #region Constructor
        public CancelTransactionManager()
        {

        }
        #endregion


        #region Operation for cancel the transactions
        public override List<string> TaskOperation(List<string> receivedInfoList, Dictionary<string, CashierRegister> cashierDict)
        {
            try
            {
                SerialPortLog.Log("Cancel Transaction Start");
                CashierRegister cashierRegister;
                string machineID = receivedInfoList[Constant.ZERO];
                string type = receivedInfoList[Constant.ONE];
                string productID = receivedInfoList[Constant.TWO];
                Int32 quantity = Convert.ToInt32(receivedInfoList[Constant.THREE]);
                SerialPortLog.LogOperation("Cancel Transaction: " + machineID);
                SerialPortLog.LogOperation("Barcode: " + productID);
                if (cashierDict.TryGetValue(machineID, out cashierRegister))
                {
                    return CancelTransaction(cashierRegister, productID, machineID, type, quantity);
                }
                else
                {
                    //return Constant.LOGIN_FIRST_MSG + Constant.NEWLINE;
                    List<string> output = new List<string>();
                    output.Add(machineID);
                    output.Add(type);
                    output.Add(Constant.FAIL);
                    SerialPortLog.LogOperation("Cancel Transaction Fail");
                    SerialPortLog.Log("Cancel Transaction Fail");
                    return output;
                }
            }
            catch
            {
                //return Constant.PROGRAM_ERROR + Constant.NEWLINE;
                SerialPortLog.LogOperation("Cancel Transaction Error");
                SerialPortLog.Log("Cancel Transaction Error");
                throw;
            }
        }

        private List<string> CancelTransaction(CashierRegister cashierRegister, string productID, string machineID, string type, Int32 quanlity)
        {
            if (cashierRegister.isTransactionStarted())
            {
                Transaction tr = cashierRegister.getTransactionData(productID);
                int unitSold = cashierRegister.getUnitSold(productID);
                if (unitSold == 0)
                {
                    List<string> output = new List<string>();
                    output.Add(machineID);
                    output.Add(type);
                    output.Add(Constant.FAIL);
                    SerialPortLog.LogOperation("Cancel Transaction Fail");
                    SerialPortLog.Log("Cancel Transaction Fail");
                    return output;
                }
                if (unitSold == quanlity)
                {
                    if (cashierRegister.CancelTransaction(productID))
                    {
                        List<string> output = new List<string>();
                        output.Add(machineID);
                        output.Add(type);
                        output.Add(Constant.SUCCESS);
                        output.Add(tr.ProductName);
                        output.Add(tr.SellingPrice.ToString());
                        SerialPortLog.LogOperation("Cancel Transaction Successfully");
                        SerialPortLog.Log("Cancel Transaction Successfully");
                        return output;
                        //return Constant.CANCEL_TRANSACTION_MSG + productID;
                    }
                    else
                    {
                        List<string> output = new List<string>();
                        output.Add(machineID);
                        output.Add(type);
                        output.Add(Constant.FAIL);
                        SerialPortLog.LogOperation("Cancel Transaction Fail");
                        SerialPortLog.Log("Cancel Transaction Fail");
                        return output;
                    }
                }
                else if (unitSold > quanlity)
                {
                    //update new quantity for product
                    //Transaction tr = cashierRegister.getTransactionData(productID);
                    if (cashierRegister.updateQuantity(productID, quanlity))
                    {
                        //cashierRegister.CancelTransaction(productID);
                        //cashierRegister.AddTransaction(productID, tr.ProductName, tr.BatchID, tr.CostPirce, tr.SellingPrice, tr.Discount, quanlity);
                        //double deductTotal = quanlity * Convert.ToInt32(tr.SellingPrice);
                        //deductTotal = (double)Math.Round(Convert.ToDecimal(deductTotal), Constant.TWO);
                        List<string> output = new List<string>();
                        output.Add(machineID);
                        output.Add(type);
                        output.Add(Constant.SUCCESS);
                        output.Add(tr.ProductName);
                        output.Add(tr.SellingPrice.ToString());
                        SerialPortLog.LogOperation("Cancel Transaction Successfully");
                        SerialPortLog.Log("Cancel Transaction Successfully");
                        return output;
                    }
                    else
                    {
                        List<string> output = new List<string>();
                        output.Add(machineID);
                        output.Add(type);
                        output.Add(Constant.FAIL);
                        SerialPortLog.LogOperation("Cancel Transaction Fail");
                        SerialPortLog.Log("Cancel Transaction Fail");
                        return output;
                    }
                }
                else
                {
                     List<string> output = new List<string>();
                     output.Add(machineID);
                     output.Add(type);
                     output.Add(Constant.FAIL);
                     SerialPortLog.LogOperation("Cancel Transaction Fail");
                     SerialPortLog.Log("Cancel Transaction Fail");
                     return output;
                }
            }
            else
            {
                List<string> output = new List<string>();
                output.Add(machineID);
                output.Add(type);
                output.Add(Constant.FAIL);
                SerialPortLog.LogOperation("Cancel Transaction Fail");
                SerialPortLog.Log("Cancel Transaction Fail");
                return output;
                //return Constant.START_TRANSACTION_FIRST + Constant.NEWLINE;
            }
        }
        #endregion

    }
}
